1. Project Clover database Tue Apr 11 2023 12:41:06 EDT
  2. Package org.joda.time.chrono

File AssembledChronology.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
70% of files have more coverage

Code metrics

162
178
48
2
562
430
137
0.77
3.71
24
2.85

Classes

Class Line # Actions
AssembledChronology 35 104 0% 96 79
0.6652542466.5%
AssembledChronology.Fields 393 74 0% 41 73
0.519736852%
 

Contributing tests

This file is covered by 2791 tests. .

Source view

1    /*
2    * Copyright 2001-2005 Stephen Colebourne
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package org.joda.time.chrono;
17   
18    import java.io.IOException;
19    import java.io.ObjectInputStream;
20   
21    import org.joda.time.Chronology;
22    import org.joda.time.DateTimeField;
23    import org.joda.time.DateTimeZone;
24    import org.joda.time.DurationField;
25   
26    /**
27    * Abstract Chronology that enables chronologies to be assembled from
28    * a container of fields.
29    * <p>
30    * AssembledChronology is thread-safe and immutable.
31    *
32    * @author Brian S O'Neill
33    * @since 1.0
34    */
 
35    public abstract class AssembledChronology extends BaseChronology {
36   
37    private static final long serialVersionUID = -6728465968995518215L;
38   
39    private final Chronology iBase;
40    private final Object iParam;
41   
42    private transient DurationField iMillis;
43    private transient DurationField iSeconds;
44    private transient DurationField iMinutes;
45    private transient DurationField iHours;
46    private transient DurationField iHalfdays;
47   
48    private transient DurationField iDays;
49    private transient DurationField iWeeks;
50    private transient DurationField iWeekyears;
51    private transient DurationField iMonths;
52    private transient DurationField iYears;
53    private transient DurationField iCenturies;
54    private transient DurationField iEras;
55   
56    private transient DateTimeField iMillisOfSecond;
57    private transient DateTimeField iMillisOfDay;
58    private transient DateTimeField iSecondOfMinute;
59    private transient DateTimeField iSecondOfDay;
60    private transient DateTimeField iMinuteOfHour;
61    private transient DateTimeField iMinuteOfDay;
62    private transient DateTimeField iHourOfDay;
63    private transient DateTimeField iClockhourOfDay;
64    private transient DateTimeField iHourOfHalfday;
65    private transient DateTimeField iClockhourOfHalfday;
66    private transient DateTimeField iHalfdayOfDay;
67   
68    private transient DateTimeField iDayOfWeek;
69    private transient DateTimeField iDayOfMonth;
70    private transient DateTimeField iDayOfYear;
71    private transient DateTimeField iWeekOfWeekyear;
72    private transient DateTimeField iWeekyear;
73    private transient DateTimeField iWeekyearOfCentury;
74    private transient DateTimeField iMonthOfYear;
75    private transient DateTimeField iYear;
76    private transient DateTimeField iYearOfEra;
77    private transient DateTimeField iYearOfCentury;
78    private transient DateTimeField iCenturyOfEra;
79    private transient DateTimeField iEra;
80   
81    // Bit set determines which base fields are used
82    // bit 1 set: hourOfDay, minuteOfHour, secondOfMinute, and millisOfSecond fields
83    // bit 2 set: millisOfDayField
84    // bit 3 set: year, monthOfYear, and dayOfMonth fields
85    private transient int iBaseFlags;
86   
87    /**
88    * Constructor calls the assemble method, enabling subclasses to define its
89    * supported fields. If a base chronology is supplied, the field set
90    * initially contains references to each base chronology field.
91    * <p>
92    * Other methods in this class will delegate to the base chronology, if it
93    * can be determined that the base chronology will produce the same results
94    * as AbstractChronology.
95    *
96    * @param base optional base chronology to copy initial fields from
97    * @param param optional param object available for assemble method
98    */
 
99  386 toggle protected AssembledChronology(Chronology base, Object param) {
100  386 iBase = base;
101  386 iParam = param;
102  386 setFields();
103    }
104   
 
105  95112 toggle public DateTimeZone getZone() {
106  95112 Chronology base;
107  ? if ((base = iBase) != null) {
108  95112 return base.getZone();
109    }
110  0 return null;
111    }
112   
 
113  487 toggle public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
114    int millisOfDay)
115    throws IllegalArgumentException {
116  487 Chronology base;
117  ? if ((base = iBase) != null && (iBaseFlags & 6) == 6) {
118    // Only call specialized implementation if applicable fields are the same.
119  454 return base.getDateTimeMillis(year, monthOfYear, dayOfMonth, millisOfDay);
120    }
121  33 return super.getDateTimeMillis(year, monthOfYear, dayOfMonth, millisOfDay);
122    }
123   
 
124  4295 toggle public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
125    int hourOfDay, int minuteOfHour,
126    int secondOfMinute, int millisOfSecond)
127    throws IllegalArgumentException {
128  4295 Chronology base;
129  ? if ((base = iBase) != null && (iBaseFlags & 5) == 5) {
130    // Only call specialized implementation if applicable fields are the same.
131  4146 return base.getDateTimeMillis(year, monthOfYear, dayOfMonth,
132    hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
133    }
134  149 return super.getDateTimeMillis(year, monthOfYear, dayOfMonth,
135    hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
136    }
137   
 
138  480 toggle public long getDateTimeMillis(long instant,
139    int hourOfDay, int minuteOfHour,
140    int secondOfMinute, int millisOfSecond)
141    throws IllegalArgumentException {
142  480 Chronology base;
143  ? if ((base = iBase) != null && (iBaseFlags & 1) == 1) {
144    // Only call specialized implementation if applicable fields are the same.
145  247 return base.getDateTimeMillis
146    (instant, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
147    }
148  233 return super.getDateTimeMillis
149    (instant, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
150    }
151   
 
152  834 toggle public final DurationField millis() {
153  834 return iMillis;
154    }
155   
 
156  359269 toggle public final DateTimeField millisOfSecond() {
157  359269 return iMillisOfSecond;
158    }
159   
 
160  7078856 toggle public final DateTimeField millisOfDay() {
161  7078856 return iMillisOfDay;
162    }
163   
 
164  864 toggle public final DurationField seconds() {
165  864 return iSeconds;
166    }
167   
 
168  16527 toggle public final DateTimeField secondOfMinute() {
169  16527 return iSecondOfMinute;
170    }
171   
 
172  1960 toggle public final DateTimeField secondOfDay() {
173  1960 return iSecondOfDay;
174    }
175   
 
176  995 toggle public final DurationField minutes() {
177  995 return iMinutes;
178    }
179   
 
180  23238 toggle public final DateTimeField minuteOfHour() {
181  23238 return iMinuteOfHour;
182    }
183   
 
184  529 toggle public final DateTimeField minuteOfDay() {
185  529 return iMinuteOfDay;
186    }
187   
 
188  1180 toggle public final DurationField hours() {
189  1180 return iHours;
190    }
191   
 
192  20054 toggle public final DateTimeField hourOfDay() {
193  20054 return iHourOfDay;
194    }
195   
 
196  497 toggle public final DateTimeField clockhourOfDay() {
197  497 return iClockhourOfDay;
198    }
199   
 
200  537 toggle public final DurationField halfdays() {
201  537 return iHalfdays;
202    }
203   
 
204  499 toggle public final DateTimeField hourOfHalfday() {
205  499 return iHourOfHalfday;
206    }
207   
 
208  501 toggle public final DateTimeField clockhourOfHalfday() {
209  501 return iClockhourOfHalfday;
210    }
211   
 
212  803 toggle public final DateTimeField halfdayOfDay() {
213  803 return iHalfdayOfDay;
214    }
215   
 
216  62385 toggle public final DurationField days() {
217  62385 return iDays;
218    }
219   
 
220  2594936 toggle public final DateTimeField dayOfWeek() {
221  2594936 return iDayOfWeek;
222    }
223   
 
224  4111828 toggle public final DateTimeField dayOfMonth() {
225  4111828 return iDayOfMonth;
226    }
227   
 
228  3075 toggle public final DateTimeField dayOfYear() {
229  3075 return iDayOfYear;
230    }
231   
 
232  1138 toggle public final DurationField weeks() {
233  1138 return iWeeks;
234    }
235   
 
236  1117 toggle public final DateTimeField weekOfWeekyear() {
237  1117 return iWeekOfWeekyear;
238    }
239   
 
240  746 toggle public final DurationField weekyears() {
241  746 return iWeekyears;
242    }
243   
 
244  1215 toggle public final DateTimeField weekyear() {
245  1215 return iWeekyear;
246    }
247   
 
248  498 toggle public final DateTimeField weekyearOfCentury() {
249  498 return iWeekyearOfCentury;
250    }
251   
 
252  48466 toggle public final DurationField months() {
253  48466 return iMonths;
254    }
255   
 
256  3097460 toggle public final DateTimeField monthOfYear() {
257  3097460 return iMonthOfYear;
258    }
259   
 
260  40892 toggle public final DurationField years() {
261  40892 return iYears;
262    }
263   
 
264  6415933 toggle public final DateTimeField year() {
265  6415933 return iYear;
266    }
267   
 
268  595 toggle public final DateTimeField yearOfEra() {
269  595 return iYearOfEra;
270    }
271   
 
272  571 toggle public final DateTimeField yearOfCentury() {
273  571 return iYearOfCentury;
274    }
275   
 
276  553 toggle public final DurationField centuries() {
277  553 return iCenturies;
278    }
279   
 
280  632 toggle public final DateTimeField centuryOfEra() {
281  632 return iCenturyOfEra;
282    }
283   
 
284  721 toggle public final DurationField eras() {
285  721 return iEras;
286    }
287   
 
288  638 toggle public final DateTimeField era() {
289  638 return iEra;
290    }
291   
292    /**
293    * Invoked by the constructor and after deserialization to allow subclasses
294    * to define all of its supported fields. All unset fields default to
295    * unsupported instances.
296    *
297    * @param fields container of fields
298    */
299    protected abstract void assemble(Fields fields);
300   
301    /**
302    * Returns the same base chronology as passed into the constructor.
303    */
 
304  135766 toggle protected final Chronology getBase() {
305  135766 return iBase;
306    }
307   
308    /**
309    * Returns the same param object as passed into the constructor.
310    */
 
311  18753 toggle protected final Object getParam() {
312  18753 return iParam;
313    }
314   
 
315  485 toggle private void setFields() {
316  485 Fields fields = new Fields();
317  485 if (iBase != null) {
318  432 fields.copyFieldsFrom(iBase);
319    }
320  485 assemble(fields);
321   
322    {
323  485 DurationField f;
324  ? iMillis = (f = fields.millis) != null ? f : super.millis();
325  ? iSeconds = (f = fields.seconds) != null ? f : super.seconds();
326  ? iMinutes = (f = fields.minutes) != null ? f : super.minutes();
327  ? iHours = (f = fields.hours) != null ? f : super.hours();
328  ? iHalfdays = (f = fields.halfdays) != null ? f : super.halfdays();
329  ? iDays = (f = fields.days) != null ? f : super.days();
330  ? iWeeks = (f = fields.weeks) != null ? f : super.weeks();
331  ? iWeekyears = (f = fields.weekyears) != null ? f : super.weekyears();
332  ? iMonths = (f = fields.months) != null ? f : super.months();
333  ? iYears = (f = fields.years) != null ? f : super.years();
334  ? iCenturies = (f = fields.centuries) != null ? f : super.centuries();
335  ? iEras = (f = fields.eras) != null ? f : super.eras();
336    }
337   
338    {
339  485 DateTimeField f;
340  ? iMillisOfSecond = (f = fields.millisOfSecond) != null ? f : super.millisOfSecond();
341  ? iMillisOfDay = (f = fields.millisOfDay) != null ? f : super.millisOfDay();
342  ? iSecondOfMinute = (f = fields.secondOfMinute) != null ? f : super.secondOfMinute();
343  ? iSecondOfDay = (f = fields.secondOfDay) != null ? f : super.secondOfDay();
344  ? iMinuteOfHour = (f = fields.minuteOfHour) != null ? f : super.minuteOfHour();
345  ? iMinuteOfDay = (f = fields.minuteOfDay) != null ? f : super.minuteOfDay();
346  ? iHourOfDay = (f = fields.hourOfDay) != null ? f : super.hourOfDay();
347  ? iClockhourOfDay = (f = fields.clockhourOfDay) != null ? f : super.clockhourOfDay();
348  ? iHourOfHalfday = (f = fields.hourOfHalfday) != null ? f : super.hourOfHalfday();
349  ? iClockhourOfHalfday = (f = fields.clockhourOfHalfday) != null ? f : super.clockhourOfHalfday();
350  ? iHalfdayOfDay = (f = fields.halfdayOfDay) != null ? f : super.halfdayOfDay();
351  ? iDayOfWeek = (f = fields.dayOfWeek) != null ? f : super.dayOfWeek();
352  ? iDayOfMonth = (f = fields.dayOfMonth) != null ? f : super.dayOfMonth();
353  ? iDayOfYear = (f = fields.dayOfYear) != null ? f : super.dayOfYear();
354  ? iWeekOfWeekyear = (f = fields.weekOfWeekyear) != null ? f : super.weekOfWeekyear();
355  ? iWeekyear = (f = fields.weekyear) != null ? f : super.weekyear();
356  ? iWeekyearOfCentury = (f = fields.weekyearOfCentury) != null ? f : super.weekyearOfCentury();
357  ? iMonthOfYear = (f = fields.monthOfYear) != null ? f : super.monthOfYear();
358  ? iYear = (f = fields.year) != null ? f : super.year();
359  ? iYearOfEra = (f = fields.yearOfEra) != null ? f : super.yearOfEra();
360  ? iYearOfCentury = (f = fields.yearOfCentury) != null ? f : super.yearOfCentury();
361  ? iCenturyOfEra = (f = fields.centuryOfEra) != null ? f : super.centuryOfEra();
362  ? iEra = (f = fields.era) != null ? f : super.era();
363    }
364   
365  485 int flags;
366  485 if (iBase == null) {
367  53 flags = 0;
368    } else {
369  432 flags =
370  432 ((iHourOfDay == iBase.hourOfDay() &&
371    iMinuteOfHour == iBase.minuteOfHour() &&
372    iSecondOfMinute == iBase.secondOfMinute() &&
373    iMillisOfSecond == iBase.millisOfSecond()) ? 1 : 0) |
374   
375  432 ((iMillisOfDay == iBase.millisOfDay()) ? 2 : 0) |
376   
377  432 ((iYear == iBase.year() &&
378    iMonthOfYear == iBase.monthOfYear() &&
379    iDayOfMonth == iBase.dayOfMonth()) ? 4 : 0);
380    }
381   
382  485 iBaseFlags = flags;
383    }
384   
 
385  99 toggle private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
386  99 in.defaultReadObject();
387  99 setFields();
388    }
389   
390    /**
391    * A container of fields used for assembling a chronology.
392    */
 
393    public static final class Fields {
394    public DurationField millis;
395    public DurationField seconds;
396    public DurationField minutes;
397    public DurationField hours;
398    public DurationField halfdays;
399   
400    public DurationField days;
401    public DurationField weeks;
402    public DurationField weekyears;
403    public DurationField months;
404    public DurationField years;
405    public DurationField centuries;
406    public DurationField eras;
407   
408    public DateTimeField millisOfSecond;
409    public DateTimeField millisOfDay;
410    public DateTimeField secondOfMinute;
411    public DateTimeField secondOfDay;
412    public DateTimeField minuteOfHour;
413    public DateTimeField minuteOfDay;
414    public DateTimeField hourOfDay;
415    public DateTimeField clockhourOfDay;
416    public DateTimeField hourOfHalfday;
417    public DateTimeField clockhourOfHalfday;
418    public DateTimeField halfdayOfDay;
419   
420    public DateTimeField dayOfWeek;
421    public DateTimeField dayOfMonth;
422    public DateTimeField dayOfYear;
423    public DateTimeField weekOfWeekyear;
424    public DateTimeField weekyear;
425    public DateTimeField weekyearOfCentury;
426    public DateTimeField monthOfYear;
427    public DateTimeField year;
428    public DateTimeField yearOfEra;
429    public DateTimeField yearOfCentury;
430    public DateTimeField centuryOfEra;
431    public DateTimeField era;
432   
 
433  485 toggle Fields() {
434    }
435   
436    /**
437    * Copy the supported fields from a chronology into this container.
438    */
 
439  446 toggle public void copyFieldsFrom(Chronology chrono) {
440    {
441  446 DurationField f;
442  ? if (isSupported(f = chrono.millis())) {
443  446 millis = f;
444    }
445  ? if (isSupported(f = chrono.seconds())) {
446  446 seconds = f;
447    }
448  ? if (isSupported(f = chrono.minutes())) {
449  446 minutes = f;
450    }
451  ? if (isSupported(f = chrono.hours())) {
452  446 hours = f;
453    }
454  ? if (isSupported(f = chrono.halfdays())) {
455  446 halfdays = f;
456    }
457  ? if (isSupported(f = chrono.days())) {
458  446 days = f;
459    }
460  ? if (isSupported(f = chrono.weeks())) {
461  446 weeks = f;
462    }
463  ? if (isSupported(f = chrono.weekyears())) {
464  446 weekyears = f;
465    }
466  ? if (isSupported(f = chrono.months())) {
467  446 months = f;
468    }
469  ? if (isSupported(f = chrono.years())) {
470  446 years = f;
471    }
472  ? if (isSupported(f = chrono.centuries())) {
473  446 centuries = f;
474    }
475  ? if (isSupported(f = chrono.eras())) {
476  0 eras = f;
477    }
478    }
479   
480    {
481  446 DateTimeField f;
482  ? if (isSupported(f = chrono.millisOfSecond())) {
483  446 millisOfSecond = f;
484    }
485  ? if (isSupported(f = chrono.millisOfDay())) {
486  446 millisOfDay = f;
487    }
488  ? if (isSupported(f = chrono.secondOfMinute())) {
489  446 secondOfMinute = f;
490    }
491  ? if (isSupported(f = chrono.secondOfDay())) {
492  446 secondOfDay = f;
493    }
494  ? if (isSupported(f = chrono.minuteOfHour())) {
495  446 minuteOfHour = f;
496    }
497  ? if (isSupported(f = chrono.minuteOfDay())) {
498  446 minuteOfDay = f;
499    }
500  ? if (isSupported(f = chrono.hourOfDay())) {
501  446 hourOfDay = f;
502    }
503  ? if (isSupported(f = chrono.clockhourOfDay())) {
504  446 clockhourOfDay = f;
505    }
506  ? if (isSupported(f = chrono.hourOfHalfday())) {
507  446 hourOfHalfday = f;
508    }
509  ? if (isSupported(f = chrono.clockhourOfHalfday())) {
510  446 clockhourOfHalfday = f;
511    }
512  ? if (isSupported(f = chrono.halfdayOfDay())) {
513  446 halfdayOfDay = f;
514    }
515  ? if (isSupported(f = chrono.dayOfWeek())) {
516  446 dayOfWeek = f;
517    }
518  ? if (isSupported(f = chrono.dayOfMonth())) {
519  446 dayOfMonth = f;
520    }
521  ? if (isSupported(f = chrono.dayOfYear())) {
522  446 dayOfYear = f;
523    }
524  ? if (isSupported(f = chrono.weekOfWeekyear())) {
525  446 weekOfWeekyear = f;
526    }
527  ? if (isSupported(f = chrono.weekyear())) {
528  446 weekyear = f;
529    }
530  ? if (isSupported(f = chrono.weekyearOfCentury())) {
531  446 weekyearOfCentury = f;
532    }
533  ? if (isSupported(f = chrono.monthOfYear())) {
534  446 monthOfYear = f;
535    }
536  ? if (isSupported(f = chrono.year())) {
537  446 year = f;
538    }
539  ? if (isSupported(f = chrono.yearOfEra())) {
540  446 yearOfEra = f;
541    }
542  ? if (isSupported(f = chrono.yearOfCentury())) {
543  446 yearOfCentury = f;
544    }
545  ? if (isSupported(f = chrono.centuryOfEra())) {
546  446 centuryOfEra = f;
547    }
548  ? if (isSupported(f = chrono.era())) {
549  446 era = f;
550    }
551    }
552    }
553   
 
554  5352 toggle private static boolean isSupported(DurationField field) {
555  5352 return field == null ? false : field.isSupported();
556    }
557   
 
558  10258 toggle private static boolean isSupported(DateTimeField field) {
559  10258 return field == null ? false : field.isSupported();
560    }
561    }
562    }